package com.wswy.wyjk.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import com.blankj.utilcode.util.CacheDiskUtils;

public class Tools {

  private volatile static CacheDiskUtils sDefaultCache = null;

  public static CacheDiskUtils getCache(){
    if (sDefaultCache == null){
      synchronized (Tools.class){
        if (sDefaultCache == null){
          sDefaultCache = CacheDiskUtils.getInstance();
        }
      }
    }
    return sDefaultCache;
  }


  public static int evaluateColor(int startInt, int endInt,float fraction) {
    float startA = ((startInt >> 24) & 0xff) / 255.0f;
    float startR = ((startInt >> 16) & 0xff) / 255.0f;
    float startG = ((startInt >>  8) & 0xff) / 255.0f;
    float startB = ( startInt        & 0xff) / 255.0f;

    float endA = ((endInt >> 24) & 0xff) / 255.0f;
    float endR = ((endInt >> 16) & 0xff) / 255.0f;
    float endG = ((endInt >>  8) & 0xff) / 255.0f;
    float endB = ( endInt        & 0xff) / 255.0f;

    // convert from sRGB to linear
    startR = (float) Math.pow(startR, 2.2);
    startG = (float) Math.pow(startG, 2.2);
    startB = (float) Math.pow(startB, 2.2);

    endR = (float) Math.pow(endR, 2.2);
    endG = (float) Math.pow(endG, 2.2);
    endB = (float) Math.pow(endB, 2.2);

    // compute the interpolated color in linear space
    float a = startA + fraction * (endA - startA);
    float r = startR + fraction * (endR - startR);
    float g = startG + fraction * (endG - startG);
    float b = startB + fraction * (endB - startB);

    // convert back to sRGB in the [0..255] range
    a = a * 255.0f;
    r = (float) Math.pow(r, 1.0 / 2.2) * 255.0f;
    g = (float) Math.pow(g, 1.0 / 2.2) * 255.0f;
    b = (float) Math.pow(b, 1.0 / 2.2) * 255.0f;

    return Math.round(a) << 24 | Math.round(r) << 16 | Math.round(g) << 8 | Math.round(b);
  }





  public static void startCallPhone(Context context,String phone){
    try{
      Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone));
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(intent);
    }catch (Throwable e){
      e.printStackTrace();
    }
  }



  public static String formatSecondTime(long time){
    long m = time / 60;
    long s =time % 60;
    StringBuilder sb = new StringBuilder();
    if (m<10){
      sb.append(0);
    }
    sb.append(m);
    sb.append(':');

    if (s < 10){
      sb.append(0);
    }
    sb.append(s);

    return sb.toString();
  }

}
